

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="沧海横流，尽显英雄本色；激浊扬清，正是猛士当时">
  <meta name="author" content="closer">
  <meta name="keywords" content="">
  
  <title>爬虫实战 - closer的自留地</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.6.0/styles/androidstudio.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"blog.zsaa.top","root":"/","version":"1.8.10","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"always","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"baidu":"608f2baddd361128381ad2bf9377bf89","google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":"YzLqNtMw1YEwwACli1FUsIUM-gzGzoHsz","app_key":"HLUt5izfTvTcbEbOrA59W92a","server_url":"https://yzlqntmw.lc-cn-n1-shared.com"}}};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>Hello</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('https://closer_laps.coding.net/p/picture/d/picture/git/raw/master/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/Python/python.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="爬虫实战">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-05-12 23:26" pubdate>
        2020年5月12日 晚上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      4.1k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      71
       分钟
    </span>
  

  
  
    
      <!-- LeanCloud 统计文章PV -->
      <span id="leancloud-page-views-container" class="post-meta" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="leancloud-page-views"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">爬虫实战</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：2020年5月31日 凌晨
                
              </p>
            
            <div class="markdown-body">
              <h2 id="一：-requests"><a href="#一：-requests" class="headerlink" title="一： requests"></a>一： requests</h2><h3 id="1-1-豆瓣影讯之xpath"><a href="#1-1-豆瓣影讯之xpath" class="headerlink" title="1.1 豆瓣影讯之xpath"></a>1.1 豆瓣影讯之xpath</h3><figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-comment">#!/usr/bin/python3</span><br><span class="hljs-comment"># -*- coding: utf-8 -*-</span><br><span class="hljs-keyword">from</span> lxml <span class="hljs-keyword">import</span> etree<br><span class="hljs-keyword">import</span> requests<br><br><span class="hljs-comment">## 1.获取目标网站源码</span><br>headers = &#123;<br>    <span class="hljs-string">&#x27;User-Agent&#x27;</span>: <span class="hljs-string">&quot;Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50&quot;</span>,<br>    <span class="hljs-string">&#x27;Referer&#x27;</span>: <span class="hljs-string">&quot;https://cn.bing.com/&quot;</span><br>&#125;<br><br>url = <span class="hljs-string">&#x27;https://movie.douban.com/cinema/nowplaying/beijing/&#x27;</span><br>res = requests.get(url, headers=headers)<br><br><span class="hljs-comment"># 网页源码存为text</span><br>text = res.text<br><br><span class="hljs-comment"># response.text 返回的是Unicode格式的数据</span><br><span class="hljs-comment"># response.content返回的字节流数据，是bytes类型</span><br><br><br><span class="hljs-comment">## 2. 按一定规则提取数据</span><br><br><span class="hljs-comment"># 从字符串常量解析HTML文档。</span><br>html = etree.HTML(text)<br><br><span class="hljs-comment"># 获取正在上映[0],即将上映[1]。通过xpath表达式来定位class=&#x27;lists&#x27;</span><br>ul = html.xpath(<span class="hljs-string">&quot;//ul[@class=&#x27;lists&#x27;]&quot;</span>)[<span class="hljs-number">0</span>]<br><br><span class="hljs-comment"># print(etree.tostring(ul, encoding=&#x27;utf-8&#x27;).decode(&#x27;utf-8&#x27;))</span><br><br><span class="hljs-comment"># 通过xpath表达式来定位li标签，存为lis列表</span><br>lis = ul.xpath(<span class="hljs-string">&quot;./li&quot;</span>)<br><br><span class="hljs-comment">#构建movies字典</span><br>movies = []<br><br><span class="hljs-keyword">for</span> li <span class="hljs-keyword">in</span> lis:<br>    <span class="hljs-comment"># print(etree.tostring(li, encoding=&#x27;utf-8&#x27;).decode(&#x27;utf-8&#x27;))</span><br>    title = li.xpath(<span class="hljs-string">&quot;@data-title&quot;</span>)<br>    score = li.xpath(<span class="hljs-string">&quot;@data-score&quot;</span>)<br>    duration = li.xpath(<span class="hljs-string">&quot;@data-duration&quot;</span>)<br>    region = li.xpath(<span class="hljs-string">&quot;@data-region&quot;</span>)<br>    direcotr = li.xpath(<span class="hljs-string">&quot;@data-direcotr&quot;</span>)<br>    actors = li.xpath(<span class="hljs-string">&quot;@data-actors&quot;</span>)<br>    thumbnail = li.xpath(<span class="hljs-string">&quot;.//img/@src&quot;</span>)<br>    movie = &#123;<br>        <span class="hljs-string">&#x27;title&#x27;</span>: title,<br>        <span class="hljs-string">&#x27;score&#x27;</span>: score,<br>        <span class="hljs-string">&#x27;duration&#x27;</span>: duration,<br>        <span class="hljs-string">&#x27;region&#x27;</span>: region,<br>        <span class="hljs-string">&#x27;direcotr&#x27;</span>: direcotr,<br>        <span class="hljs-string">&#x27;actors&#x27;</span>: actors,<br>        <span class="hljs-string">&#x27;thumbnail&#x27;</span>: thumbnail<br>    &#125;<br>    movies.append(movie)<br><br><span class="hljs-keyword">for</span> movie <span class="hljs-keyword">in</span> movies:<br>    <span class="hljs-built_in">print</span>(movie)<br></code></pre></div></td></tr></table></figure>

<h3 id="1-2-电影天堂之xpath"><a href="#1-2-电影天堂之xpath" class="headerlink" title="1.2 电影天堂之xpath"></a>1.2 电影天堂之xpath</h3><figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-comment">#!/usr/bin/python3</span><br><span class="hljs-comment"># -*- coding: utf-8 -*-</span><br><span class="hljs-keyword">from</span> lxml <span class="hljs-keyword">import</span> etree<br><span class="hljs-keyword">import</span> requests<br><br>BASE_DOMAIN = <span class="hljs-string">&#x27;https://dytt8.net&#x27;</span><br><br>HEADERS = &#123;<br>    <span class="hljs-string">&#x27;User-Agent&#x27;</span>: <span class="hljs-string">&quot;Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50&quot;</span>,<br>    <span class="hljs-string">&#x27;Referer&#x27;</span>: <span class="hljs-string">&quot;http://whois.chinaz.com/dytt8.net&quot;</span><br>&#125;<br><br><span class="hljs-comment"># 获取每一部电影详情页的url</span><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_detail_urls</span>(<span class="hljs-params">url</span>):</span><br>    res = requests.get(url, headers=HEADERS)<br>    <span class="hljs-comment"># requests库，会默认使用自己猜测得编码方式进行解码，有时会猜错，需要手动指定解码</span><br>    text = res.content.decode(<span class="hljs-string">&#x27;gbk&#x27;</span>, errors=<span class="hljs-string">&#x27;ignore&#x27;</span>)<br>    html = etree.HTML(text)  <span class="hljs-comment"># &lt;Element html at 0x168712c4540&gt;</span><br>    detail_urls = html.xpath(<span class="hljs-string">&quot;//table[@class=&#x27;tbspan&#x27;]//a/@href&quot;</span>)<br><br>    <span class="hljs-comment"># 注释掉得等同于下面得列表表达式</span><br>    <span class="hljs-comment"># def abc(url):</span><br>    <span class="hljs-comment">#     return BASE_DOMAIN+url</span><br>    <span class="hljs-comment"># index = 0</span><br>    <span class="hljs-comment"># for detail_url in detail_urls:</span><br>    <span class="hljs-comment">#     detail_url = abc(detail_url)</span><br>    <span class="hljs-comment">#     detail_urls[index] = detail_url</span><br>    <span class="hljs-comment">#     index += 1</span><br><br>    <span class="hljs-comment"># 列表表达式</span><br>    <span class="hljs-comment"># map() 会根据提供的函数对指定序列做映射。</span><br>    <span class="hljs-comment"># 处理详情页的url，获取的url加上BASE_DOMAIN才是正确的url</span><br>    detail_urls = <span class="hljs-built_in">map</span>(<span class="hljs-keyword">lambda</span> url: BASE_DOMAIN + url, detail_urls)<br><br>    <span class="hljs-keyword">return</span> detail_urls<br><br><span class="hljs-comment"># 解析详情页面</span><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">parse_detail_page</span>(<span class="hljs-params">url</span>):</span><br>    movie = &#123;&#125;<br>    response = requests.get(url, headers=HEADERS)<br>    text = response.content.decode(<span class="hljs-string">&#x27;gbk&#x27;</span>, errors=<span class="hljs-string">&#x27;ignore&#x27;</span>)<br>    html = etree.HTML(text)<br>    <span class="hljs-comment"># 标题，返回一个字典，加上0就正常了</span><br>    <span class="hljs-comment"># title = html.xpath(&quot;//div[@class=&#x27;title_all&#x27;]//font[@color=&#x27;#07519a&#x27;]/text()&quot;)[0]</span><br>    title = html.xpath(<span class="hljs-string">&quot;//h1/font/text()&quot;</span>)[<span class="hljs-number">0</span>]<br>    movie[<span class="hljs-string">&#x27;title&#x27;</span>] = title<br>    <span class="hljs-comment"># 海报</span><br>    zoomE = html.xpath(<span class="hljs-string">&quot;//div[@id=&#x27;Zoom&#x27;]&quot;</span>)[<span class="hljs-number">0</span>]<br>    imgs = zoomE.xpath(<span class="hljs-string">&quot;.//img/@src&quot;</span>)<br>    cover = imgs[<span class="hljs-number">0</span>]<br>    movie[<span class="hljs-string">&#x27;cover&#x27;</span>] = cover<br>    <span class="hljs-comment"># 详情</span><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">parse_info</span>(<span class="hljs-params">info, rule</span>):</span><br>        <span class="hljs-keyword">return</span> info.replace(rule, <span class="hljs-string">&quot;&quot;</span>).strip()<br>    infos = zoomE.xpath(<span class="hljs-string">&quot;.//text()&quot;</span>)<br>    <span class="hljs-keyword">for</span> index, info <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(infos):<br>        <span class="hljs-keyword">if</span> info.startswith(<span class="hljs-string">&quot;◎年　　代&quot;</span>):<br>            <span class="hljs-comment"># 把&#x27;◎年　　代&#x27;替换掉，strip()消除year前后空格</span><br>            info = parse_info(info, <span class="hljs-string">&quot;◎年　　代&quot;</span>)<br>            movie[<span class="hljs-string">&#x27;year&#x27;</span>] = info<br>        <span class="hljs-keyword">elif</span> info.startswith(<span class="hljs-string">&quot;◎产　　地&quot;</span>):<br>            info = parse_info(info, <span class="hljs-string">&quot;◎产　　地&quot;</span>)<br>            movie[<span class="hljs-string">&#x27;country&#x27;</span>] = info<br>        <span class="hljs-keyword">elif</span> info.startswith(<span class="hljs-string">&quot;◎类　　别&quot;</span>):<br>            info = parse_info(info, <span class="hljs-string">&quot;◎类　　别&quot;</span>)<br>            movie[<span class="hljs-string">&#x27;category&#x27;</span>] = info<br>        <span class="hljs-keyword">elif</span> info.startswith(<span class="hljs-string">&quot;◎豆瓣评分&quot;</span>):<br>            info = parse_info(info, <span class="hljs-string">&quot;◎豆瓣评分&quot;</span>)<br>            movie[<span class="hljs-string">&#x27;douban_rating&#x27;</span>] = info<br>        <span class="hljs-keyword">elif</span> info.startswith(<span class="hljs-string">&quot;◎片　　长&quot;</span>):<br>            info = parse_info(info, <span class="hljs-string">&quot;◎片　　长&quot;</span>)<br>            movie[<span class="hljs-string">&#x27;duration&#x27;</span>] = info<br>        <span class="hljs-keyword">elif</span> info.startswith(<span class="hljs-string">&quot;◎导　　演&quot;</span>):<br>            info = parse_info(info, <span class="hljs-string">&quot;◎导　　演&quot;</span>)<br>            movie[<span class="hljs-string">&#x27;director&#x27;</span>] = info<br>        <span class="hljs-keyword">elif</span> info.startswith(<span class="hljs-string">&quot;◎主　　演&quot;</span>):<br>            info = parse_info(info, <span class="hljs-string">&quot;◎主　　演&quot;</span>)<br>            actors = [info]<br>            <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(index + <span class="hljs-number">1</span>, <span class="hljs-built_in">len</span>(infos)):<br>                actor = infos[x].strip()<br>                <span class="hljs-keyword">if</span> actor.startswith(<span class="hljs-string">&quot;◎&quot;</span>):<br>                    <span class="hljs-keyword">break</span><br>                actors.append(actor)<br>            movie[<span class="hljs-string">&#x27;actors&#x27;</span>] = info<br>        <span class="hljs-keyword">elif</span> info.startswith(<span class="hljs-string">&quot;◎简　　介&quot;</span>):<br>            info = infos[index + <span class="hljs-number">1</span>].strip()<br>            movie[<span class="hljs-string">&quot;profile&quot;</span>] = info<br>    <span class="hljs-comment"># 下载地址</span><br>    downloadUrl = html.xpath(<span class="hljs-string">&quot;//td[@bgcolor=&#x27;#fdfddf&#x27;]/a/text()&quot;</span>)[<span class="hljs-number">0</span>]<br>    movie[<span class="hljs-string">&quot;downloadUrl&quot;</span>] = downloadUrl<br>    <span class="hljs-keyword">return</span> movie<br><br><span class="hljs-comment"># 爬取</span><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">spider</span>():</span><br>    base_url = <span class="hljs-string">&#x27;https://dytt8.net/html/gndy/dyzz/list_23_&#123;&#125;.html&#x27;</span><br>    <span class="hljs-comment"># 获取1到n页</span><br>    movies = []<br>    <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>):<br>        <span class="hljs-comment"># format 格式化函数 x替代&#123;&#125;</span><br>        url = base_url.<span class="hljs-built_in">format</span>(x)<br>        detail_urls = get_detail_urls(url)<br>        <span class="hljs-comment"># 遍历详情页内信息</span><br>        <span class="hljs-keyword">for</span> detail_url <span class="hljs-keyword">in</span> detail_urls:<br>            movie = parse_detail_page(detail_url)<br>            movies.append(movie)<br>            <span class="hljs-built_in">print</span>(movie)<br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:<br>    spider()<br></code></pre></div></td></tr></table></figure>

<h3 id="1-3-古诗文之正则表达式re"><a href="#1-3-古诗文之正则表达式re" class="headerlink" title="1.3 古诗文之正则表达式re"></a>1.3 古诗文之正则表达式re</h3><figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-comment">#!/usr/bin/python3</span><br><span class="hljs-comment"># -*- coding: utf-8 -*-</span><br><span class="hljs-keyword">import</span> requests<br><span class="hljs-keyword">import</span> re<br><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">parse_page</span>(<span class="hljs-params">url</span>):</span><br>    headers = &#123;<br>        <span class="hljs-string">&#x27;User-Agent&#x27;</span>: <span class="hljs-string">&quot;Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50&quot;</span><br>    &#125;<br>    response = requests.get(url, headers)<br>    text = response.text<br>    <span class="hljs-comment"># 获取古诗文标题</span><br>    <span class="hljs-comment"># .不能匹配\n，所以加上re.DOTALL</span><br>    titles = re.findall(<span class="hljs-string">r&#x27;&lt;div\sclass=&quot;cont&quot;&gt;.*?&lt;b&gt;(.*?)&lt;/b&gt;&#x27;</span>, text, re.DOTALL)<br>    <span class="hljs-comment"># 朝代 ,re.S == re.DOTALL</span><br>    dynasties = re.findall(<span class="hljs-string">r&#x27;&lt;p class=&quot;source&quot;&gt;.*?&lt;a.*?&gt;(.*?)&lt;/a&gt;&#x27;</span>, text, re.S)<br>    <span class="hljs-comment"># 作者</span><br>    authors = re.findall(<span class="hljs-string">r&#x27;&lt;p class=&quot;source&quot;&gt;.*?&lt;a.*?&gt;.*?&lt;a.*?&gt;(.*?)&lt;/a&gt;&#x27;</span>,  text, re.S)<br>    <span class="hljs-comment"># 诗文正文</span><br>    content_tags = re.findall(<span class="hljs-string">r&#x27;&lt;div class=&quot;contson&quot; .*?&gt;(.*?)&lt;/div&gt;&#x27;</span>,  text, re.S)<br>    contents = []<br>    <span class="hljs-keyword">for</span> content <span class="hljs-keyword">in</span> content_tags:<br>        <span class="hljs-comment"># .strip()去掉换行</span><br>        content = re.sub(<span class="hljs-string">r&#x27;&lt;.*?&gt;|\n&#x27;</span>, <span class="hljs-string">&quot;&quot;</span>, content).strip()<br>        contents.append(content)<br>    <span class="hljs-comment"># 整理格式</span><br>    poems = []<br>    <span class="hljs-keyword">for</span> value <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(titles, dynasties, authors, contents):<br>        title, dynasty, author, content = value<br>        poem = &#123;<br>            <span class="hljs-string">&#x27;title&#x27;</span>: title,<br>            <span class="hljs-string">&#x27;dynasty&#x27;</span>: dynasty,<br>            <span class="hljs-string">&#x27;author&#x27;</span>: author,<br>            <span class="hljs-string">&#x27;content&#x27;</span>: content<br>        &#125;<br>        poems.append(poem)<br>    <span class="hljs-keyword">for</span> poem <span class="hljs-keyword">in</span> poems:<br>        <span class="hljs-built_in">print</span>(poem)<br><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span>():</span><br>    url = <span class="hljs-string">&#x27;https://www.gushiwen.org/default_1.aspx&#x27;</span><br>    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">1</span>, <span class="hljs-number">4</span>):<br>        url = <span class="hljs-string">&#x27;https://www.gushiwen.org/default_%s.aspx&#x27;</span> % i<br>        parse_page(url)<br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    main()<br></code></pre></div></td></tr></table></figure>

<h2 id="二：-文件存储之json和csv"><a href="#二：-文件存储之json和csv" class="headerlink" title="二： 文件存储之json和csv"></a>二： 文件存储之json和csv</h2><h3 id="2-1-json"><a href="#2-1-json" class="headerlink" title="2.1 json"></a>2.1 json</h3><figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-comment">#!/usr/bin/python3</span><br><span class="hljs-comment"># -*- coding: utf-8 -*-</span><br><span class="hljs-keyword">import</span> json<br><br>persons = [<br>    &#123;<br>        <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;张三&#x27;</span>,<br>        <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">20</span>,<br>        <span class="hljs-string">&#x27;city&#x27;</span>: <span class="hljs-string">&#x27;beijing&#x27;</span><br>    &#125;,<br>    &#123;<br>        <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;李四&#x27;</span>,<br>        <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">20</span>,<br>        <span class="hljs-string">&#x27;city&#x27;</span>: <span class="hljs-string">&#x27;shanghai&#x27;</span><br>    &#125;<br>]<br><span class="hljs-comment"># 打印用dumps，存为json文件用dump，有中文则需指定ensure_ascii=False</span><br><span class="hljs-built_in">print</span>(json.dumps(persons, ensure_ascii=<span class="hljs-literal">False</span>))<br><span class="hljs-keyword">with</span> <span class="hljs-built_in">open</span>(<span class="hljs-string">&#x27;persons.json&#x27;</span>, <span class="hljs-string">&#x27;w&#x27;</span>, encoding=<span class="hljs-string">&#x27;utf-8&#x27;</span>) <span class="hljs-keyword">as</span> fp:<br>    json.dump(persons, fp, ensure_ascii=<span class="hljs-literal">False</span>)<br><br><span class="hljs-comment"># 读取json文件</span><br><span class="hljs-keyword">with</span> <span class="hljs-built_in">open</span>(<span class="hljs-string">&#x27;persons.json&#x27;</span>, <span class="hljs-string">&#x27;r&#x27;</span>, encoding=<span class="hljs-string">&#x27;utf-8&#x27;</span>) <span class="hljs-keyword">as</span> fp:<br>    persons = json.load(fp)<br>    <span class="hljs-built_in">print</span>(persons)<br></code></pre></div></td></tr></table></figure>

<h3 id="2-2-csv"><a href="#2-2-csv" class="headerlink" title="2.2 csv"></a>2.2 csv</h3><figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-comment">#!/usr/bin/python3</span><br><span class="hljs-comment"># -*- coding: utf-8 -*-</span><br><span class="hljs-keyword">import</span> csv<br><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_list</span>():</span><br>    <span class="hljs-keyword">with</span> <span class="hljs-built_in">open</span>(<span class="hljs-string">&#x27;temp.csv&#x27;</span>, <span class="hljs-string">&#x27;r&#x27;</span>) <span class="hljs-keyword">as</span> fp:<br>        <span class="hljs-comment"># reader是一个迭代器</span><br>        reader = csv.reader(fp)<br>        <span class="hljs-comment"># 不获取第一行</span><br>        <span class="hljs-built_in">next</span>(reader)<br>        <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> reader:<br>            time = x[<span class="hljs-number">1</span>]<br>            volumn = x[-<span class="hljs-number">2</span>]<br>            <span class="hljs-built_in">print</span>(&#123;<span class="hljs-string">&#x27;time&#x27;</span>: time, <span class="hljs-string">&#x27;volumn&#x27;</span>: volumn&#125;)<br><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_dict</span>():</span><br>    <span class="hljs-keyword">with</span> <span class="hljs-built_in">open</span>(<span class="hljs-string">&#x27;temp1.csv&#x27;</span>, <span class="hljs-string">&#x27;r&#x27;</span>) <span class="hljs-keyword">as</span> fp:<br>        <span class="hljs-comment"># 不包含标题那行数据</span><br>        reader = csv.DictReader(fp)<br>        <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> reader:<br>            <span class="hljs-built_in">print</span>(x)<br><br>students = [<span class="hljs-string">&#x27;name&#x27;</span>, <span class="hljs-string">&#x27;age&#x27;</span>, <span class="hljs-string">&#x27;height&#x27;</span>]<br><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_list</span>():</span><br>    values = [<br>        &#123;<span class="hljs-string">&#x27;张三&#x27;</span>, <span class="hljs-number">18</span>, <span class="hljs-number">170</span>&#125;,<br>        &#123;<span class="hljs-string">&#x27;李四&#x27;</span>, <span class="hljs-number">20</span>, <span class="hljs-number">180</span>&#125;<br>    ]<br>    <span class="hljs-comment"># newline=&#x27;\n&#x27;为默认，连续多行则指定为空字符串</span><br>    <span class="hljs-keyword">with</span> <span class="hljs-built_in">open</span>(<span class="hljs-string">&quot;temp.csv&quot;</span>, <span class="hljs-string">&#x27;w&#x27;</span>, encoding=<span class="hljs-string">&#x27;utf-8&#x27;</span>, newline=<span class="hljs-string">&#x27;&#x27;</span>) <span class="hljs-keyword">as</span> fp:<br>        writer = csv.writer(fp)<br>        writer.writerow(students)<br>        <span class="hljs-comment"># 写入多行</span><br>        writer.writerows(values)<br><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_dict</span>():</span><br>    values = [<br>        &#123;<span class="hljs-string">&#x27;name&#x27;</span>:<span class="hljs-string">&#x27;张三&#x27;</span>, <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">18</span>, <span class="hljs-string">&#x27;height&#x27;</span>:<span class="hljs-number">170</span>&#125;,<br>        &#123;<span class="hljs-string">&#x27;name&#x27;</span>:<span class="hljs-string">&#x27;李四&#x27;</span>, <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">19</span>, <span class="hljs-string">&#x27;height&#x27;</span>:<span class="hljs-number">190</span>&#125;<br>    ]<br>    <span class="hljs-keyword">with</span> <span class="hljs-built_in">open</span>(<span class="hljs-string">&quot;temp1.csv&quot;</span>, <span class="hljs-string">&#x27;w&#x27;</span>, encoding=<span class="hljs-string">&#x27;utf-8&#x27;</span>, newline=<span class="hljs-string">&#x27;&#x27;</span>) <span class="hljs-keyword">as</span> fp:<br>        writer = csv.DictWriter(fp, students)<br>        <span class="hljs-comment"># 写入表头数据的时候，需要调用writeheader方法</span><br>        writer.writeheader()<br>        writer.writerows(values)<br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    main()<br></code></pre></div></td></tr></table></figure>

<h2 id="三：-数据库操作之MySQL"><a href="#三：-数据库操作之MySQL" class="headerlink" title="三： 数据库操作之MySQL"></a>三： 数据库操作之MySQL</h2><figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-comment">#!/usr/bin/python3</span><br><span class="hljs-comment"># -*- coding: utf-8 -*-</span><br><span class="hljs-keyword">import</span> pymysql<br><br>db = pymysql.connect(host=<span class="hljs-string">&#x27;192.168.2.4&#x27;</span>, user=<span class="hljs-string">&#x27;root&#x27;</span>, passwd=<span class="hljs-string">&#x27;root&#x27;</span>,<br>                     db=<span class="hljs-string">&#x27;test&#x27;</span>, port=<span class="hljs-number">3306</span>)<br><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">insert_db</span>(<span class="hljs-params">cursor</span>):</span><br>    <span class="hljs-comment"># 如果是变量，必须都用%s</span><br>    sql = <span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">        insert into students(class_id,name,gender,score) values(%s,%s,%s,%s)</span><br><span class="hljs-string">    &quot;&quot;&quot;</span><br>    class_id = <span class="hljs-string">&#x27;4&#x27;</span>;name = <span class="hljs-string">&#x27;小龙&#x27;</span>;gender = <span class="hljs-string">&#x27;M&#x27;</span>;score = <span class="hljs-string">&#x27;99&#x27;</span><br>    <span class="hljs-comment"># 执行sql语句</span><br>    cursor.execute(sql, (class_id, name, gender, score))<br><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">find_db</span>(<span class="hljs-params">cursor</span>):</span><br>    sql = <span class="hljs-string">&quot;&quot;&quot;select * from students&quot;&quot;&quot;</span><br>    cursor.execute(sql)<br>    <span class="hljs-comment"># print(cursor.fetchall()[-1])</span><br>    result = cursor.fetchall()<br>    <span class="hljs-keyword">for</span> temp <span class="hljs-keyword">in</span> result:<br>        <span class="hljs-built_in">print</span>(temp)<br><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">update_db</span>(<span class="hljs-params">cursor</span>):</span><br>    sql = <span class="hljs-string">&quot;&quot;&quot;update students set score=&#x27;100&#x27; where name=&#x27;小龙&#x27;&quot;&quot;&quot;</span><br>    cursor.execute(sql)<br><br><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">delete_db</span>(<span class="hljs-params">cursor</span>):</span><br>    sql = <span class="hljs-string">&quot;&quot;&quot;delete from students where name=&#x27;小龙&#x27;&quot;&quot;&quot;</span><br>    cursor.execute(sql)<br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    cursor = db.cursor()<br>    insert_db(cursor)<br>    update_db(cursor)<br>    delete_db(cursor)<br>    find_db(cursor)<br>    db.commit()<br>    db.close()<br></code></pre></div></td></tr></table></figure>

<h2 id="四：-多线程"><a href="#四：-多线程" class="headerlink" title="四： 多线程"></a>四： 多线程</h2><h3 id="4-1-多线程类操作"><a href="#4-1-多线程类操作" class="headerlink" title="4.1 多线程类操作"></a>4.1 多线程类操作</h3><figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-comment">#!/usr/bin/python3</span><br><span class="hljs-comment"># -*- coding: utf-8 -*-</span><br><span class="hljs-keyword">import</span> threading, time<br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CodingThread</span>(<span class="hljs-params">threading.Thread</span>):</span><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">run</span>(<span class="hljs-params">self</span>):</span><br>        <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">3</span>):<br>            <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;正在写代码%s&#x27;</span> % threading.current_thread())<br>            time.sleep(<span class="hljs-number">1</span>)<br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DrawingThread</span>(<span class="hljs-params">threading.Thread</span>):</span><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">run</span>(<span class="hljs-params">self</span>):</span><br>        <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">3</span>):<br>            <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;正在画图%s&#x27;</span> % threading.current_thread())<br>            time.sleep(<span class="hljs-number">1</span>)<br><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span>():</span><br>    t1 = CodingThread()<br>    t2 = DrawingThread()<br>    t1.start()<br>    t2.start()<br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    main()<br></code></pre></div></td></tr></table></figure>

<h3 id="4-2-多线程加锁解锁"><a href="#4-2-多线程加锁解锁" class="headerlink" title="4.2 多线程加锁解锁"></a>4.2 多线程加锁解锁</h3><figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-comment">#!/usr/bin/python3</span><br><span class="hljs-comment"># -*- coding: utf-8 -*-</span><br><span class="hljs-keyword">import</span> threading<br><br>VALUE = <span class="hljs-number">0</span><br>gLock = threading.Lock()<br><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add_value</span>():</span><br>    <span class="hljs-keyword">global</span> VALUE<br>    <span class="hljs-comment"># 加锁</span><br>    gLock.acquire()<br>    <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">1000000</span>):<br>        VALUE += <span class="hljs-number">1</span><br>    <span class="hljs-comment"># 解锁</span><br>    gLock.release()<br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;value: %d&#x27;</span> % VALUE)<br><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span>():</span><br>    <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">2</span>):<br>        <span class="hljs-comment"># 注意函数名后面不要加括号，加上括号返回的则是函数的返回值</span><br>        t = threading.Thread(target=add_value)<br>        t.start()<br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    main()<br></code></pre></div></td></tr></table></figure>

<h3 id="4-3-Condition锁"><a href="#4-3-Condition锁" class="headerlink" title="4.3 Condition锁"></a>4.3 Condition锁</h3><figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-keyword">import</span> threading<br><span class="hljs-keyword">import</span> random<br><span class="hljs-keyword">import</span> time<br><br>gMoney = <span class="hljs-number">1000</span><br>gCondition = threading.Condition()<br>gTimes = <span class="hljs-number">0</span><br>gTotalTimes = <span class="hljs-number">5</span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Producer</span>(<span class="hljs-params">threading.Thread</span>):</span><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">run</span>(<span class="hljs-params">self</span>):</span><br>        <span class="hljs-keyword">global</span> gMoney<br>        <span class="hljs-keyword">global</span> gCondition<br>        <span class="hljs-keyword">global</span> gTimes<br>        <span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>:<br>            money = random.randint(<span class="hljs-number">100</span>, <span class="hljs-number">1000</span>)<br>            gCondition.acquire()<br>            <span class="hljs-keyword">if</span> gTimes &gt;= gTotalTimes:<br>                gCondition.release()<br>                <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;当前生产者总共生产了%s次&#x27;</span> % gTimes)<br>                <span class="hljs-keyword">break</span><br>            gMoney += money<br>            <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;%s当前存入%s元钱，剩余%s元钱&#x27;</span> % (threading.current_thread(), money, gMoney))<br>            gTimes += <span class="hljs-number">1</span><br>            time.sleep(<span class="hljs-number">0.5</span>)<br>            gCondition.notify_all()<br>            gCondition.release()<br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Consumer</span>(<span class="hljs-params">threading.Thread</span>):</span><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">run</span>(<span class="hljs-params">self</span>):</span><br>        <span class="hljs-keyword">global</span> gMoney<br>        <span class="hljs-keyword">global</span> gCondition<br>        <span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>:<br>            money = random.randint(<span class="hljs-number">100</span>, <span class="hljs-number">500</span>)<br>            gCondition.acquire()<br>            <span class="hljs-comment"># 这里要给个while循环判断，因为等轮到这个线程的时候</span><br>            <span class="hljs-comment"># 条件有可能又不满足了</span><br>            <span class="hljs-keyword">while</span> gMoney &lt; money:<br>                <span class="hljs-keyword">if</span> gTimes &gt;= gTotalTimes:<br>                    gCondition.release()<br>                    <span class="hljs-keyword">return</span><br>                <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;%s准备取%s元钱，剩余%s元钱，不足！&#x27;</span> % (threading.current_thread(), money, gMoney))<br>                gCondition.wait()<br>            gMoney -= money<br>            <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;%s当前取出%s元钱，剩余%s元钱&#x27;</span> % (threading.current_thread(), money, gMoney))<br>            time.sleep(<span class="hljs-number">0.5</span>)<br>            gCondition.release()<br><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span>():</span><br>    <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">5</span>):<br>        Consumer(name=<span class="hljs-string">&#x27;消费者线程%d&#x27;</span> % x).start()<br><br>    <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">2</span>):<br>        Producer(name=<span class="hljs-string">&#x27;生产者线程%d&#x27;</span> % x).start()<br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    main()<br></code></pre></div></td></tr></table></figure>

<h3 id="4-4-斗图啦"><a href="#4-4-斗图啦" class="headerlink" title="4.4 斗图啦"></a>4.4 斗图啦</h3><figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-keyword">import</span> threading<br><span class="hljs-keyword">import</span> requests<br><span class="hljs-keyword">from</span> lxml <span class="hljs-keyword">import</span> etree<br><span class="hljs-keyword">from</span> urllib <span class="hljs-keyword">import</span> request<br><span class="hljs-keyword">import</span> os<br><span class="hljs-keyword">import</span> re<br><span class="hljs-keyword">from</span> queue <span class="hljs-keyword">import</span> Queue<br><br><span class="hljs-comment"># 获取每个表情的url</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Producer</span>(<span class="hljs-params">threading.Thread</span>):</span><br>    headers = &#123;<br>        <span class="hljs-string">&#x27;User-Agent&#x27;</span>: <span class="hljs-string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36&#x27;</span><br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, page_queue, img_queue, *args, **kwargs</span>):</span><br>        <span class="hljs-built_in">super</span>(Producer, self).__init__(*args, **kwargs)<br>        self.page_queue = page_queue<br>        self.img_queue = img_queue<br><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">run</span>(<span class="hljs-params">self</span>):</span><br>        <span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>:<br>            <span class="hljs-keyword">if</span> self.page_queue.empty():<br>                <span class="hljs-keyword">break</span><br>            url = self.page_queue.get()<br>            self.parse_page(url)<br><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">parse_page</span>(<span class="hljs-params">self, url</span>):</span><br>        response = requests.get(url, headers=self.headers)<br>        text = response.text<br>        html = etree.HTML(text)<br>        <span class="hljs-comment"># 获取class不等于gif的图片内容</span><br>        imgs = html.xpath(<span class="hljs-string">&quot;//div[@class=&#x27;page-content text-center&#x27;]//img[@class!=&#x27;gif&#x27;]&quot;</span>)<br>        <span class="hljs-keyword">for</span> img <span class="hljs-keyword">in</span> imgs:<br>            img_url = img.get(<span class="hljs-string">&#x27;data-original&#x27;</span>)<br>            <span class="hljs-comment"># img_url = img.xpath(&quot;.//@data-original&quot;)[0]</span><br>            <span class="hljs-comment"># 提取图片url的后缀名</span><br>            suffix = os.path.splitext(img_url)[<span class="hljs-number">1</span>]<br>            <span class="hljs-comment"># suffix = &#x27;.&#x27; + img_url.split(&#x27;.&#x27;)[-1]</span><br>            alt = img.get(<span class="hljs-string">&#x27;alt&#x27;</span>)<br>            <span class="hljs-comment"># alt = img.xpath(&quot;.//@alt&quot;)[0]</span><br>            <span class="hljs-comment"># 替换掉文件名中的，。？?,/\\·等不支持的文件名字符</span><br>            alt = re.sub(<span class="hljs-string">r&#x27;[，。？?,/\\·*！《]&#x27;</span>, <span class="hljs-string">&#x27;&#x27;</span>, alt)<br>            img_name = alt + suffix<br>            <span class="hljs-comment"># 图片的url和name放入队列</span><br>            self.img_queue.put((img_url, img_name))<br><br><span class="hljs-comment"># 下载表情</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Consumer</span>(<span class="hljs-params">threading.Thread</span>):</span><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, page_queue, img_queue, *args, **kwargs</span>):</span><br>        <span class="hljs-built_in">super</span>(Consumer, self).__init__(*args, **kwargs)<br>        self.page_queue = page_queue<br>        self.img_queue = img_queue<br><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">run</span>(<span class="hljs-params">self</span>):</span><br>        <span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>:<br>            <span class="hljs-keyword">if</span> self.img_queue.empty() <span class="hljs-keyword">and</span> self.page_queue.empty():<br>                <span class="hljs-keyword">return</span><br>            url, filename = self.img_queue.get(block=<span class="hljs-literal">True</span>)<br>            <span class="hljs-comment"># 下载图片放入images目录下</span><br>            request.urlretrieve(url, <span class="hljs-string">&#x27;images/&#x27;</span> + filename)<br>            <span class="hljs-built_in">print</span>(filename + <span class="hljs-string">&#x27;  下载完成！&#x27;</span>)<br><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span>():</span><br>    page_queue = Queue(<span class="hljs-number">100</span>)<br>    img_queue = Queue(<span class="hljs-number">500</span>)<br>    <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>):<br>        url = <span class="hljs-string">&quot;http://www.doutula.com/photo/list/?page=%d&quot;</span> % x<br>        <span class="hljs-comment"># url 添加到队列中</span><br>        page_queue.put(url)<br><br>    <span class="hljs-comment"># 判断是否创建images目录用于存放图片</span><br>    <span class="hljs-keyword">if</span> os.path.exists(<span class="hljs-string">&#x27;images/&#x27;</span>) <span class="hljs-keyword">is</span> <span class="hljs-literal">False</span>:<br>        os.makedirs(<span class="hljs-string">&#x27;images/&#x27;</span>)<br><br>    <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">5</span>):<br>        t = Producer(page_queue, img_queue)<br>        t.start()<br><br>    <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">5</span>):<br>        t = Consumer(page_queue, img_queue)<br>        t.start()<br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    main()<br></code></pre></div></td></tr></table></figure>

<h2 id="五：-selenium"><a href="#五：-selenium" class="headerlink" title="五： selenium"></a>五： selenium</h2><figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-comment">#!/usr/bin/python3</span><br><span class="hljs-comment"># -*- coding: utf-8 -*-</span><br><span class="hljs-keyword">import</span> time<br><span class="hljs-keyword">import</span> random<br><span class="hljs-keyword">from</span> selenium <span class="hljs-keyword">import</span> webdriver<br><span class="hljs-keyword">import</span> xlwt<br><span class="hljs-keyword">from</span> selenium.webdriver.chrome.options <span class="hljs-keyword">import</span> Options<br><span class="hljs-keyword">from</span> selenium.webdriver.common.by <span class="hljs-keyword">import</span> By<br><span class="hljs-keyword">from</span> selenium.webdriver.support.ui <span class="hljs-keyword">import</span> WebDriverWait<br><span class="hljs-keyword">from</span> selenium.webdriver.support <span class="hljs-keyword">import</span> expected_conditions <span class="hljs-keyword">as</span> EC<br><br>frontCar = <span class="hljs-string">&#x27;1234&#x27;</span><br>countNum = <span class="hljs-number">0</span><br><span class="hljs-comment"># 此处改为具体姓名信息</span><br>class_one = [<span class="hljs-string">&#x27;小马&#x27;</span>]<br>class_two = [<span class="hljs-string">&#x27;大飞&#x27;</span>, <span class="hljs-string">&#x27;小猪&#x27;</span>]<br>class_three = [<span class="hljs-string">&#x27;李四&#x27;</span>, <span class="hljs-string">&#x27;王五&#x27;</span>]<br>class_four = [<span class="hljs-string">&quot;张三&quot;</span>]<br><br><span class="hljs-comment"># 登录模块</span><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">DengLu</span>(<span class="hljs-params">driver</span>):</span><br>    <span class="hljs-comment"># 访问网页</span><br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;即将打开登录界面&#x27;</span>)<br><br>    <span class="hljs-comment"># 平台登录界面 # 此处改为查验url</span><br>    driver.get(<span class="hljs-string">&quot;你的查验url&quot;</span>)<br><br>    <span class="hljs-comment"># 输入账号 # 改为你的账号</span><br>    driver.find_element_by_xpath(<span class="hljs-string">&#x27;/html/body/ngx-app/rt-login/div[2]/div[2]/input&#x27;</span>).send_keys(<br>        <span class="hljs-string">&quot;改为你的账号&quot;</span>)<br><br>    <span class="hljs-comment"># 输入密码</span><br>    driver.find_element_by_xpath(<span class="hljs-string">&#x27;/html/body/ngx-app/rt-login/div[2]/div[3]/input&#x27;</span>).send_keys(<span class="hljs-string">&quot;改为你的密码&quot;</span>)<br><br>    <span class="hljs-comment"># 手动输入验证码并登录</span><br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;请手动输入图片验证码并登录...&quot;</span>)<br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;登录后请点开需要查验的第一辆...&quot;</span>)<br><br>    <span class="hljs-comment"># 输入拉取次数</span><br>    <span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>:<br>        <span class="hljs-keyword">try</span>:<br>            <span class="hljs-comment"># 如果输入是一个整数，开始拉取数据</span><br>            n = <span class="hljs-built_in">int</span>(<span class="hljs-built_in">input</span>(<span class="hljs-string">&#x27;请输入拉取得数据个数：&#x27;</span>))<br>            <span class="hljs-keyword">if</span> n &gt; <span class="hljs-number">5000</span>:<br>                <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;数字太大了,请重新输入:&quot;</span>)<br>                <span class="hljs-keyword">continue</span><br>            <span class="hljs-keyword">break</span><br>        <span class="hljs-keyword">except</span> ValueError:<br>            <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;输入的不是整数，请重新输入:&#x27;</span>)<br><br>    <span class="hljs-keyword">return</span> n<br><br><br><span class="hljs-comment"># 获取数据模块</span><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">StartGetDate</span>(<span class="hljs-params">driver, fp, sheet, i, n</span>):</span><br>    <span class="hljs-comment"># 全局变量frontCar</span><br>    <span class="hljs-keyword">global</span> frontCar, countNum<br><br>    <span class="hljs-comment"># 查验时间</span><br>    ChaYanFullTime = driver.find_element_by_xpath(<br>        <span class="hljs-string">&#x27;//*[@id=&quot;app-root&quot;]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[7]/td[5]/span&#x27;</span>).text<br><br>    <span class="hljs-comment"># 牌</span><br>    chepai = driver.find_element_by_xpath(<br>        <span class="hljs-string">&#x27;//*[@id=&quot;app-root&quot;]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[5]/td[3]/span&#x27;</span>).text<br><br>    localCar = ChaYanFullTime + chepai<br><br>    <span class="hljs-comment"># 如何当前和上一辆一致，则获取上一辆失败，再次点击</span><br>    <span class="hljs-keyword">while</span> frontCar == localCar:<br>        <span class="hljs-keyword">try</span>:<br>            <span class="hljs-comment"># 点击上一辆以供查看</span><br>            driver.find_element_by_xpath(<br>                <span class="hljs-string">&#x27;//*[@id=&quot;app-root&quot;]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/div[2]/img[1]&#x27;</span>).click()<br>            <span class="hljs-comment"># 等待牌加载完成</span><br>            element = WebDriverWait(driver, <span class="hljs-number">60</span>).until(<br>                EC.presence_of_element_located((By.XPATH,<br>                                                <span class="hljs-string">&#x27;//*[@id=&quot;app-root&quot;]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[5]/td[3]/span&#x27;</span>))<br>            )<br>            <span class="hljs-comment"># 等待时间加载完成</span><br>            element = WebDriverWait(driver, <span class="hljs-number">60</span>).until(<br>                EC.presence_of_element_located((By.XPATH,<br>                                                <span class="hljs-string">&#x27;//*[@id=&quot;app-root&quot;]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[7]/td[5]/span&#x27;</span>))<br>            )<br>            <span class="hljs-comment"># 等待上一辆填充完成</span><br>            element = WebDriverWait(driver, <span class="hljs-number">60</span>).until(<br>                EC.presence_of_element_located((By.XPATH,<br>                                                <span class="hljs-string">&#x27;//*[@id=&quot;app-root&quot;]/app-main-layout&#x27;</span>))<br>            )<br>            time.sleep(<span class="hljs-number">1</span>)<br>        <span class="hljs-keyword">except</span> find_element_by_class_xpath:<br>            <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;已经全部拉取完毕。&quot;</span>)<br>            <span class="hljs-comment"># driver.quit()</span><br>        <span class="hljs-keyword">finally</span>:<br>            time.sleep(random.randint(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>))<br><br>        <span class="hljs-comment"># 查验时间</span><br>        ChaYanFullTime = driver.find_element_by_xpath(<br>            <span class="hljs-string">&#x27;//*[@id=&quot;app-root&quot;]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[7]/td[5]/span&#x27;</span>).text<br><br>        <span class="hljs-comment"># 牌</span><br>        chepai = driver.find_element_by_xpath(<br>            <span class="hljs-string">&#x27;//*[@id=&quot;app-root&quot;]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[5]/td[3]/span&#x27;</span>).text<br><br>        localCar = ChaYanFullTime + chepai<br><br>    <span class="hljs-comment"># 查验日期</span><br>    ChaYanDate = ChaYanFullTime.split(<span class="hljs-string">&#x27; &#x27;</span>)[<span class="hljs-number">0</span>]<br><br>    <span class="hljs-comment"># 查验时间</span><br>    ChaYanTime = ChaYanFullTime.split(<span class="hljs-string">&#x27; &#x27;</span>)[-<span class="hljs-number">1</span>]<br><br>    <span class="hljs-comment"># 辆类型</span><br>    cheliangleixing = driver.find_element_by_xpath(<br>        <span class="hljs-string">&#x27;//*[@id=&quot;app-root&quot;]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[5]/td[5]/span&#x27;</span>).text<br><br>    <span class="hljs-comment"># 金额</span><br>    jinE = driver.find_element_by_xpath(<br>        <span class="hljs-string">&#x27;//*[@id=&quot;app-root&quot;]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[8]/td[3]/span&#x27;</span>).text<br><br>    shouFeiYuan = driver.find_element_by_xpath(<br>        <span class="hljs-string">&#x27;//*[@id=&quot;app-root&quot;]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[11]/td[7]/span&#x27;</span>).text<br><br>    <span class="hljs-comment"># 稽查员</span><br>    jiChaYuan = driver.find_element_by_xpath(<br>        <span class="hljs-string">&#x27;//*[@id=&quot;app-root&quot;]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[12]/td[3]/span&#x27;</span>).text<br><br>    <span class="hljs-comment"># 班次</span><br>    <span class="hljs-keyword">if</span> jiChaYuan <span class="hljs-keyword">in</span> class_three:<br>        banci = <span class="hljs-number">3</span><br>    <span class="hljs-keyword">elif</span> jiChaYuan <span class="hljs-keyword">in</span> class_one:<br>        banci = <span class="hljs-number">1</span><br>    <span class="hljs-keyword">elif</span> jiChaYuan <span class="hljs-keyword">in</span> class_two:<br>        banci = <span class="hljs-number">2</span><br>    <span class="hljs-keyword">elif</span> jiChaYuan <span class="hljs-keyword">in</span> class_four:<br>        banci = <span class="hljs-number">4</span><br>    <span class="hljs-keyword">else</span>:<br>        banci = <span class="hljs-literal">None</span><br><br>    <span class="hljs-comment"># 查验结果</span><br>    chayanResult = driver.find_element_by_xpath(<br>        <span class="hljs-string">&#x27;//*[@id=&quot;app-root&quot;]/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[9]/td[3]/span&#x27;</span>).text<br><br>    <span class="hljs-comment"># 开始打印</span><br>    <span class="hljs-built_in">print</span>(i + <span class="hljs-number">1</span>, <span class="hljs-string">&quot;/&quot;</span>, n, <span class="hljs-string">&#x27;:  &#x27;</span>, banci, ChaYanFullTime, shouFeiYuan, jiChaYuan, chepai, cheliangleixing, jinE, end=<span class="hljs-string">&#x27; &#x27;</span>)<br>    <span class="hljs-built_in">print</span>(ChaYanFullTime, banci, shouFeiYuan, jiChaYuan, chepai, cheliangleixing, jinE, end=<span class="hljs-string">&#x27; &#x27;</span>, file=fp)<br><br>    <span class="hljs-comment"># 写入不同得数据</span><br>    sheet.write(i + <span class="hljs-number">2</span>, <span class="hljs-number">0</span>, i + <span class="hljs-number">1</span>)<br>    sheet.write(i + <span class="hljs-number">2</span>, <span class="hljs-number">1</span>, ChaYanDate)<br>    sheet.write(i + <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, ChaYanTime)<br>    sheet.write(i + <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, banci)<br>    <span class="hljs-comment"># sheet.write(i + 2, 4, daoKou)</span><br>    sheet.write(i + <span class="hljs-number">2</span>, <span class="hljs-number">5</span>, shouFeiYuan)<br>    sheet.write(i + <span class="hljs-number">2</span>, <span class="hljs-number">6</span>, jiChaYuan)<br>    sheet.write(i + <span class="hljs-number">2</span>, <span class="hljs-number">7</span>, chepai)<br>    sheet.write(i + <span class="hljs-number">2</span>, <span class="hljs-number">8</span>, cheliangleixing)<br><br>    <span class="hljs-comment"># 不合格则添加不符原因</span><br>    <span class="hljs-keyword">if</span> chayanResult == <span class="hljs-string">&#x27;不合格&#x27;</span>:<br>        <span class="hljs-comment"># 不合格原因</span><br>        buhege = driver.find_element_by_xpath(<br>            <span class="hljs-string">&#x27;/html/body/ngx-app/app-main-layout/div/div/div/app-check-manage/app-check-manage-detial/div/app-ireport/div/div[2]/table/tbody/tr[10]/td[3]/span&#x27;</span>).text<br>        <span class="hljs-built_in">print</span>(buhege, end=<span class="hljs-string">&#x27; &#x27;</span>)<br>        <span class="hljs-built_in">print</span>(buhege, end=<span class="hljs-string">&#x27; &#x27;</span>, file=fp)<br>        sheet.write(i + <span class="hljs-number">2</span>, <span class="hljs-number">9</span>, buhege)<br>    <span class="hljs-keyword">else</span>:<br>        <span class="hljs-built_in">print</span>(end=<span class="hljs-string">&#x27; &#x27;</span>)<br>        <span class="hljs-built_in">print</span>(end=<span class="hljs-string">&#x27; &#x27;</span>, file=fp)<br>        sheet.write(i + <span class="hljs-number">2</span>, <span class="hljs-number">9</span>, <span class="hljs-string">&#x27;&#x27;</span>)<br><br>    sheet.write(i + <span class="hljs-number">2</span>, <span class="hljs-number">10</span>, jinE)<br><br>    <span class="hljs-comment"># 换行</span><br>    <span class="hljs-built_in">print</span>(end=<span class="hljs-string">&#x27;\n&#x27;</span>)<br>    <span class="hljs-built_in">print</span>(end=<span class="hljs-string">&#x27;\n&#x27;</span>, file=fp)<br><br>    <span class="hljs-comment"># 后赋予前</span><br>    frontCar = localCar<br><br>    <span class="hljs-comment"># 统计打印数量</span><br>    countNum = countNum + <span class="hljs-number">1</span><br><br><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span>():</span><br><br>    <span class="hljs-comment"># 实例化一个启动参数对象,获取chrome的配置</span><br>    options = Options()<br><br>    <span class="hljs-comment"># 设置启动浏览器窗口大小</span><br>    options.add_argument(<span class="hljs-string">&#x27;--window-size=1366,768&#x27;</span>)<br><br>    <span class="hljs-comment"># 启动浏览器</span><br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;Launch browser&#x27;</span>)<br>    <span class="hljs-comment"># 获取driver对象</span><br>    driver = webdriver.Chrome(chrome_options=options)<br><br>    <span class="hljs-comment"># 删除cookie</span><br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;Delete cookies&#x27;</span>)<br>    driver.delete_all_cookies()<br><br>    <span class="hljs-comment"># 开始登录</span><br>    n = DengLu(driver)<br><br>    <span class="hljs-comment"># 统计开始时间</span><br>    startTime = time.time()<br><br>    <span class="hljs-comment"># 写入文件</span><br>    fp = <span class="hljs-built_in">open</span>(<span class="hljs-string">r&quot;temp.txt&quot;</span>, <span class="hljs-string">&quot;w+&quot;</span>, encoding=<span class="hljs-string">&quot;utf-8&quot;</span>)<br>    <span class="hljs-comment"># 写入excel</span><br>    workbook = xlwt.Workbook()<br>    sheet = workbook.add_sheet(<span class="hljs-string">&quot;sheet1&quot;</span>)<br><br>    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(n):<br>        StartGetDate(driver, fp, sheet, i, n)<br>        <span class="hljs-keyword">if</span> i % <span class="hljs-number">10</span> == <span class="hljs-number">9</span>:<br>            <span class="hljs-comment"># 每10辆保存一次</span><br>            workbook.save(<span class="hljs-string">&#x27;temp.xls&#x27;</span>)<br><br>    <span class="hljs-comment"># 关闭文件</span><br>    fp.close()<br>    <span class="hljs-comment"># 保存为temp.xls</span><br>    workbook.save(<span class="hljs-string">&#x27;temp.xls&#x27;</span>)<br><br>    <span class="hljs-comment"># 脚本运行成功,退出浏览器</span><br>    driver.quit()<br><br>    <span class="hljs-comment"># 统计结束时间</span><br>    endTime = time.time()<br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;共 %d 辆&quot;</span> % countNum)<br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;共花费时间： %d 秒&quot;</span> % (endTime - startTime))<br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    main()<br></code></pre></div></td></tr></table></figure>

<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1aJ411C7oM">Python爬虫教程</a></p>
<p><a target="_blank" rel="noopener" href="https://selenium-python.readthedocs.io/">selenium官方docs</a></p>
<p><a target="_blank" rel="noopener" href="https://python-selenium-zh.readthedocs.io/zh_CN/latest/">selenium-python中文文档</a></p>
<p><a target="_blank" rel="noopener" href="http://scrapy-chs.readthedocs.io/zh_CN/latest/index.html">Scrapy中文文档</a></p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/python/">python</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/python/">python</a>
                    
                      <a class="hover-with-bg" href="/tags/%E7%88%AC%E5%9B%BE/">爬图</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2020/05/26/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/Github/GithubAcitonsDeployHexoBlog/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">用Github Acitons在GitHub、Gitee、Coding上自动部署Hexo</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2020/05/03/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86-%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/">
                        <span class="hidden-mobile">计算机组成原理-读书笔记</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments" lazyload>
                
                  
                
                
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js', function () {
        new Valine({
          el: "#valine",
          app_id: "YzLqNtMw1YEwwACli1FUsIUM-gzGzoHsz",
          app_key: "HLUt5izfTvTcbEbOrA59W92a",
          placeholder: "畅所欲言...",
          path: window.location.pathname,
          avatar: "robohash",
          meta: ["nick","mail","link"],
          pageSize: "10",
          lang: "zh-CN",
          highlight: true,
          recordIP: false,
          serverURLs: "",
        });
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->

  <div class="col-lg-7 mx-auto nopadding-x-md">
    <div class="container custom post-custom mx-auto">
      <img src="https://closer_laps.coding.net/p/picture/d/picture/git/raw/master/pay/pay.png" srcset="/img/loading.gif" lazyload class="rounded mx-auto d-block mt-3" style="width:355.4px; height:200px;">
    </div>
  </div>


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- LeanCloud 统计PV -->
        <span id="leancloud-site-pv-container" style="display: none">
            总访问量 
            <span id="leancloud-site-pv"></span>
             次
          </span>
      
      
        <!-- LeanCloud 统计UV -->
        <span id="leancloud-site-uv-container" style="display: none">
            总访客数 
            <span id="leancloud-site-uv"></span>
             人
          </span>
      

    
  </div>


  
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        苏ICP备20032307号
      </a>
    </span>
    
      
        <span>
          <a
            href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=32020602001023"
            rel="nofollow noopener"
            class="beian-police"
            target="_blank"
          >
            
              <span style="visibility: hidden; width: 0">|</span>
              <img src="/img/police_beian.png" srcset="/img/loading.gif" lazyload alt="police-icon"/>
            
            <span>苏公网安备 32020602001023号</span>
          </a>
        </span>
      
    
  </div>


  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.2/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.0/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js" ></script>




  <script defer src="/js/leancloud.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.11/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    (function () {
      var path = "/local-search.xml";
      $('#local-search-input').on('click', function() {
        searchFunc(path, 'local-search-input', 'local-search-result');
      });
      $('#modalSearch').on('shown.bs.modal', function() {
        $('#local-search-input').focus();
      });
    })()
  </script>





  

  
    <!-- MathJax -->
    <script>
      MathJax = {
        tex: {
          inlineMath: [['$', '$'], ['\\(', '\\)']]
        },
        options: {
          renderActions: {
            findScript: [10, doc => {
              document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
                const display = !!node.type.match(/; *mode=display/);
                const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
                const text = document.createTextNode('');
                node.parentNode.replaceChild(text, node);
                math.start = { node: text, delim: '', n: 0 };
                math.end = { node: text, delim: '', n: 0 };
                doc.math.push(math);
              });
            }, '', false],
            insertedScript: [200, () => {
              document.querySelectorAll('mjx-container').forEach(node => {
                let target = node.parentNode;
                if (target.nodeName.toLowerCase() === 'li') {
                  target.parentNode.classList.add('has-jax');
                }
              });
            }, '', false]
          }
        }
      };
    </script>

    <script async src="https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/tex-svg.js" ></script>

  








  
    <!-- Baidu Analytics -->
    <script defer>
      var _hmt = _hmt || [];
      (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?608f2baddd361128381ad2bf9377bf89";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
      })();
    </script>
  

  

  

  

  

  





<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
